jQuery(function($) {
    var doc = document,
        $doc = $(doc);

    var form = {
        moblie: false,
        paw: false,
        // msg: false
    },
        timer = {
            count: 0,
            index: 0
    }

    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    //清除错误信息
    function clearErrMessage(input, err) {
        $doc.on("focus", input, function() {
            $(err).css({"visibility": "hidden"});
        });
    }
    //是否为空
    function empty(val,key,err) {
        if (!utils.empty(val)) {
            form[key] = true;
        } else {
            form[key] = false;
            utils.errMessage(err);
        }
    }

    //是否为数字
    function isNumber(val,key,err) {
        if (utils.isNumber(val)) {
             form[key] = true;
        } else {
            form[key] = false;
            alert(err);
        }
    }

    //验证手机号 -start-
    function checkMoblie(val, key, err) {
        if (!utils.empty(val)) {
            form.moblie = true;
        } else {
            form.moblie = false;
            utils.errMessage(err);
            return;
        }

        //手机号是否为数值
        if (utils.checkMoblie($(".phone input"))) {
            form.moblie = true;
        } else {
            form.moblie = false;
            alert("请填写正确的手机号码！");
        }
    }
    //验证手机号 -end-

    //验证密码 -start-
    function checkPaw(val, key, err) {
        if (!utils.empty(val)) {
            form.paw = true;
        } else {
            form.paw = false;
            utils.errMessage(err);
        }
    }
    //验证密码 -end-

    //提交表单 -start-

    $doc.on("click", ".submit", function() {
        checkMoblie($(".phone input").val(), ".moblie", ".moblie_error");
        checkPaw($(".paw input").val(), "paw", ".paw_error");
        // checkMsgSec($(".msg input").val());
        var pwd= $(".paw input").val();
        if(pwd.length >= 6 && pwd.length<=20){
            form.paw = true;
            $(".paw input").css('border-color','#ccc');
        }else{
            form.paw = false;
            $(".paw input").css('border-color','red');
            noty({text:'手机号须6-20位字符',type:'error',timeout:500});
        }
        if (form.moblie &&
            form.paw) {
            //do something
            doLogin();
        }
       
    });

    $doc.keydown(function(event){
        if(event.keyCode==13){
            checkMoblie($(".phone input").val(), ".moblie", ".moblie_error");
            checkPaw($(".paw input").val(), "paw", ".paw_error");
            // checkMsgSec($(".msg input").val());
            var pwd= $(".paw input").val();
            if(pwd.length >= 6 && pwd.length<=20){
                form.paw = true;
                $(".paw input").css('border-color','#ccc');
            }else{
                form.paw = false;
                $(".paw input").css('border-color','red');
                noty({text:'密码须6-20位字符',type:'error',timeout:500});
            }
            if (form.moblie &&
                form.paw) {
                //do something
                doLogin();
            }
        }
    });

    /*拆分字符串*/
    function createElm(elm) {
        return doc.createElement(elm);
    }
    function createText(text) {
        return doc.createTextNode(text);
    }

    function getUserCount(param) {
        if (typeof param !== "string") {
            return false;
        }

        var fragment = doc.createDocumentFragment(),
            count = doc.querySelector(".count ul"),
            li = null,
            em = null,
            text = "";

        for (var i = 0; i < param.length; i++) {
            li = createElm("li");
            em = createElm("em");
            text = createText(param.substr(i,1));
            em.appendChild(text);
            li.appendChild(em);
            fragment.appendChild(li);
        }

        count.appendChild(fragment);
    }
    getUserCount(userCount);

    //提交表单 -end-
    clearErrMessage(".phone input", ".moblie_error");
    clearErrMessage(".paw input", ".paw_error");

    /** 发送ajax请求 */
    function doLogin(){
        var phone = $(".phone input").val();
        var password =$(".paw input").val();
        var remember = 1;

        $.ajax({
            type:"post",
            url:SignIn,
            dataType:"json",
            data:{
                "phone":phone ,
                "password":password,
                "remember":remember
            },
            success:function(obj){
                if(obj.level === "success"){
                    window.location.href=Home;
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:800});
            }
        });
    }
});